package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNPerssion;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.List;

public class PerssionListAdapter2 extends BaseAdapter {
    private List<YNPerssion> listItems;
    private LayoutInflater layoutinflater;
    private Context context;
    private int userid;
    private int groupid;

    static class ViewHolder {
        private LinearLayout perssion_item;
        private SimpleDraweeView avatar;
        private TextView name;
        private TextView open;
        private TextView perssion_item_bline;
    }

    public PerssionListAdapter2(Context context, List<YNPerssion> dataSet, int userid, int groupid) {
        this.listItems = dataSet;
        this.context = context;
        this.layoutinflater = LayoutInflater.from(context);
        this.userid = userid;
        this.groupid = groupid;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final YNPerssion ynPerssion = listItems.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutinflater.inflate(R.layout.perssionlist_listitem4, null);
            holder.perssion_item = (LinearLayout) convertView.findViewById(R.id.perssion_item);
            holder.avatar = (SimpleDraweeView) convertView.findViewById(R.id.perssion_item_avatar);
            holder.name = (TextView) convertView.findViewById(R.id.perssion_item_name);
            holder.open = (TextView) convertView.findViewById(R.id.perssion_item_open);
            holder.perssion_item_bline = (TextView) convertView.findViewById(R.id.perssion_item_bline);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageOptHelper.showFrescoAvatar(Uri.parse(ynPerssion.getUpic()), holder.avatar, context);
        final int beuserid = ynPerssion.getUserid();
        holder.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authorizesinglePerssion(beuserid);
            }
        });
        holder.perssion_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.name.setText(ynPerssion.getUnickname());
        if (position == listItems.size() - 1) {
            holder.perssion_item_bline.setVisibility(View.GONE);
        } else {
            holder.perssion_item_bline.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private void authorizesinglePerssion(int beuserid) {
        String urlsto =context.getString(R.string.SLIVER_URL)+"AuthorizeOrCancelForWatchPhoto";
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(userid));
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupid));
        mParam.put(CommonConstants.YNS_BEUSER_ID, String.valueOf(beuserid));
        mParam.put(CommonConstants.YNS_TYPE, "authorize");
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject result = new JSONObject(s);
                            if (result.optInt("code") == 0) {
                                context.sendBroadcast(new Intent(ACTIONs.actionupdatePerssionList));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(context)).executeAsync(req);
    }

}